﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChoJu.Single.DAL
{
    public class WeiCalca
    {
        private int _cacheCount = 30;
        private int _count;
        private double _max_min;
        private int _maxCount = 30;

        private int _minWeight = 5; //最小重量
        private double _max = 0;
        private bool _isDown; //是否开始往下变化
        private double _lastWeight;
        private int _idx = 0;
        private int _idxCalc = 6; //从最大值后的第几个值开始算起

        private List<double> _lstWeis;
        private List<double> _lstWeisView;
        private List<int> _indexs = new List<int>();
        private double _currentWeight;

        //测试用
        private string _dataIndex;
        private Dictionary<string, List<double>> _dict = new Dictionary<string, List<double>>();

        public WeiCalca(int count, int smoothCount, double max_min)
        {
            _count = count;
            _max_min = max_min;
            _idxCalc = smoothCount;
            _lstWeis = new List<double>();
            _lstWeisView = new List<double>();
        }

        public double Input(double weight)
        {

            double result = -100;
            if (_lstWeis.Count == _count)
            {
                var min = _lstWeis.Min();
                var max = _lstWeis.Max();
                var val = Math.Abs(max - min);
                if (val < _max_min)
                {
                    result = _lstWeis.Average();
                    _lstWeis.RemoveAt(0);
                    if(result > _count)
                    {
                        _lstWeisView.AddRange(_lstWeis);
                        _lstWeisView.Add(-100);
                    }
                }
                else
                {
                    result = -100;
                    _lstWeis.Clear();
                }
            }

            _lstWeis.Add(weight);
            return Math.Round(result, 2);
        }

        public double InputNew(double weight)
        {
            _currentWeight = weight;
            if (weight > _minWeight & weight > _max)
            {
                _max = weight;
                _isDown = false;
                _lstWeis.Clear();
                _lastWeight = 0;
                _idx = 0;
                return -100;
            }

            if(weight < _minWeight)
            {
                _max = 0;
                _isDown = false;
                return -100;
            }   
            
            if(weight <= _max & _isDown == false)
            {
                _lastWeight = weight;
                _isDown = true;
                _idx++;
                return -100;
            }

            if (_idx < _idxCalc & _isDown == true)
            {
                _idx++;
                return -100;
            }

            if (_lstWeis.Count >= _maxCount)
                _lstWeis.RemoveAt(0);

            double result = -100;
            _lstWeis.Add(weight);

            var change = _lstWeis.Max() - _lstWeis.Min();
            if (change > 10) //当集合里面的变化值大于10g，说明不稳定，不返回数据 
                return -100;

            if(_lstWeis.Count >= _count)
            {
                var temp = _lstWeis.OrderBy(o => o).ToList();
                var r = temp.Count / 4;
                for(int i = 0; i < r; i++)
                {
                    temp.RemoveAt(0);
                    temp.RemoveAt(temp.Count - 1);
                }
                result = Math.Round(temp.Average(), 1); 
                
            }

            return result;
        }
        

        bool clone = false;
        public List<double> GetData()
        {
            clone = true;
            var temp =_lstWeisView.Select(s => s).ToList();
            clone = false;
            return temp;
        }

        public double GetCurrentWeight()
        {
            return Math.Round(_currentWeight, 2);
        }

        public void Print()
        {
            StringBuilder stringBuilder = new StringBuilder();
            //_lstWeisView.ForEach(s => stringBuilder.AppendLine(s.ToString()));
            //var file = Path.Combine(AppContext.BaseDirectory, "Data", "Weight.txt");
            //File.WriteAllText(file, stringBuilder.ToString());

            foreach(var key in _dict.Keys)
            {
                foreach(var value in _dict[key])
                {
                    stringBuilder.AppendLine(value.ToString());
                }
                stringBuilder.AppendLine(key);
                stringBuilder.AppendLine();
            }

            var file = Path.Combine(AppContext.BaseDirectory, "Data", "Weight.txt");
            File.WriteAllText(file, stringBuilder.ToString());
        }

        public void Test(double weight)
        {
            _dataIndex = DateTime.Now.ToString("HHmmss") + "_" + weight.ToString() ;
            _dict.Add(_dataIndex, _lstWeis.Select(s => s).ToList());
        }
    }
}
